import { apiPost, apiPut } from './client';

export type LoginResponse = {
  accessToken: string;
  tokenType: string;
  expiresIn: number;
  user: {
    id: string;
    email: string;
    phone?: string | null;
    gender: 'MALE' | 'FEMALE' | 'UNKNOWN';
    nickname?: string | null;
    avatar?: string | null;
    createdAt: string;
    updatedAt: string;
  };
};

export type User = {
  id: string;
  email: string;
  phone?: string | null;
  gender: 'MALE' | 'FEMALE' | 'UNKNOWN';
  nickname?: string | null;
  avatar?: string | null;
  createdAt: string;
  updatedAt: string;
};

export type UpdateUserPayload = {
  email?: string;
  phone?: string | null;
  password?: string;
  confirmPassword?: string;
  gender?: 'MALE' | 'FEMALE' | 'UNKNOWN';
  nickname?: string | null;
};

export async function loginApi(identifier: string, password: string) {
  return apiPost<LoginResponse>('/auth/login', { identifier, password });
}

export type RegisterPayload = {
  email?: string;
  phone?: string;
  password: string;
  gender: 'MALE' | 'FEMALE' | 'UNKNOWN';
  nickname?: string;
};

export async function registerApi(payload: RegisterPayload) {
  return apiPost<LoginResponse>('/auth/register', payload);
}

export async function updateUserApi(payload: UpdateUserPayload) {
  return apiPut<User>('/auth/profile', payload);
}